package com.plian.system.controller.wf;

import com.plian.Tools.TokenUtil;
import com.plian.Tools.UuidUtil;
import com.plian.Tools.pagedata.PageData;
import com.plian.system.bean.wf.WFExpressionProperty;
import com.plian.system.model.CommonCode;
import com.plian.system.model.ListResult;
import com.plian.system.model.ObjResult;
import com.plian.system.service.wf.WFExpressionPropertyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("api/wf-expression-property")
public class WFExpressionPropertyController {

    private final WFExpressionPropertyService wfExpressionPropertyService;

    @Autowired
    public WFExpressionPropertyController(WFExpressionPropertyService wfExpressionPropertyService) {
        this.wfExpressionPropertyService = wfExpressionPropertyService;
    }

    @PostMapping("add")
    @Transactional
    public ObjResult add(@RequestBody PageData pd) {
        WFExpressionProperty result = new WFExpressionProperty();
        result.setId(UuidUtil.get32UUID());
        result.setName(pd.getString("name"));
        result.setType(pd.getString("type"));
        result.setOperators(pd.getString("operators"));
        result.setInput(pd.getString("input"));
        result.setUrl(pd.getString("url"));
        result.setPropertyName(pd.getString("propertyName"));
        result.setValues(pd.getString("values"));
        result.setCreateTime(new Date());
        result.setCreatorId(TokenUtil.getTokenUserId());
        wfExpressionPropertyService.addByBean(result);
        return new ObjResult(CommonCode.SUCCESS);
    }

    @PostMapping("get")
    public ObjResult get(@RequestBody PageData pd) {
        ObjResult result = new ObjResult(CommonCode.SUCCESS);
        result.setPageData(wfExpressionPropertyService.getById(pd.getString("id")));
        return result;
    }

    @PostMapping("update")
    public ObjResult update(@RequestBody PageData pd) {
        PageData result = wfExpressionPropertyService.getById(pd.getString("id"));
        result.put("name",pd.getString("name"));
        result.put("type",pd.getString("type"));
        result.put("operators",pd.getString("operators"));
        result.put("input",pd.getString("input"));
        result.put("url",pd.getString("url"));
        result.put("propertyName",pd.getString("propertyName"));
        result.put("values",pd.getString("values"));
        wfExpressionPropertyService.updateByBean(result);
        return new ObjResult(CommonCode.SUCCESS);
    }

    @PostMapping("delete")
    public ObjResult delete(@RequestBody PageData pd) {
        wfExpressionPropertyService.deleteById(pd.getString("id"));
        return new ObjResult(CommonCode.SUCCESS);
    }

    @RequestMapping("delete-batch")
    @Transactional
    public ObjResult deleteBatch(@RequestBody PageData pd) {
        wfExpressionPropertyService.deleteByIdBatch((List<String>) pd.get("ids"));
        return new ObjResult(CommonCode.SUCCESS);
    }

    @RequestMapping("get-all")
//    @PassToken
    public ListResult<WFExpressionProperty> getAll() {
        ListResult<WFExpressionProperty> result = new ListResult<>(CommonCode.SUCCESS);
        result.setList(wfExpressionPropertyService.getAll());
        return result;
    }
}
